<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    ringData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const data = [
        {
          name: '成都',
          value: 23.5
        },
        {
          name: '南充',
          value: 51.5
        },
        {
          name: '绵阳',
          value: 34.5
        }
      ]
      // key-value映射
      const myMap = {}
      for (let i = 0; i < data.length; i++) {
        myMap[data[i].name] = data[i].value
      }
      var dw = '万人次'
      var seriesName = '测试'
      var colors = ['#3dd2df', '#1dc78b', '#bb4dff', '#f95600', '#ffe400', '#22a4f8']
      for (let i = 0; i < data.length; i++) {
        data[i].itemStyle = {
          normal: {
            color: colors[i % colors.length],
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          }
        }
      }
      const title = '#94e5ff'
      var legend = {
        top: 'middle',
        right: '1%',
        orient: 'vertical',
        textStyle: {
          color: title,
          fontSize: 14
        },
        formatter: function(a, b, c) {
          let result = a
          result += myMap[a] + dw
          return result
        }
      }

      var series = [
        {
          name: seriesName,
          type: 'pie',
          radius: ['38%', '52%'],
          center: ['38%', '56%'],
          selectedMode: 'single',
          avoidLabelOverlap: false,
          label: {
            fontSize: 16,
            lineHeight: 20,
            position: 'center', // 高亮区域
            backgroundColor: '#1f2447',
            formatter: function(params) { // 默认显示第一个数据
              /* if (params.dataIndex === 0) {

              } else {
                return ''
              }*/
              return '{p|' + params.percent + '%}' + '\n{nm|' + params.name + '}'
            },
            /* emphasis: {
              formatter: function(params) {
                if (params.dataIndex !== 0) {
                  return '{p|' + params.percent + '%}' + '\n{nm|' + params.name + '}'
                }
              }
            },*/
            rich: {
              p: {
                color: title,
                fontSize: 18
                /* backgroundColor: '#1f2447' // 覆盖index=0时的数据*/
              },
              nm: {
                color: title,
                fontSize: 12
                /* backgroundColor: '#1f2447' // 覆盖index=0时的数据*/
              }
            }
          },
          data: data
        }
      ]
      const option = {
        tooltip: {
          trigger: 'item'
        },
        legend: legend,
        series: series
      }

      this.chart.setOption(
        option
        , true)
    }
  }
}
</script>

<style>

</style>
